<?php
/**
 * Created by PhpStorm.
 * User: tao
 * Date: 15-4-4
 * Time: 下午10:17
 */
class SessionManager{
    private $reids;
    private $sessionSavePath;
    private $sessionName;
    private $sessionExprireTime=30;

    public function __construct(){
        $this->reids=new Redis();
        $this->reids->connect("127.0.0.1",6379);

        //php.ini中必须把session.save_hadler设置为user;
        
        $retval=session_set_save_handler(
            array($this,"open"),
            array($this,"close"),
            array($this,"read"),
            array($this,"write"),
            array($this,"destory"),
            array($this,"gc")
        );

       // session_set_save_handler("open","close","read","write","destory","gc");

        session_start();
    }


    public function open($path,$name){
        $this->sessionSavePath=$path;
        $this->sessionName=$name;
        return true;
    }

    public function close(){
        return true;
    }

    public function read($id){
        $value=$this->reids->get($id);
        if($value)
           return $value;
        else
            return '';
    }

    public function write($id,$data){
        if($this->reids->set($id,$data)){
            $this->reids->expire($id,$this->sessionExprireTime);
            return true;
        }
        return false;
    }

    public function destory($id){
        if($this->reids->delete($id))
          return true;
        else
            return false;
    }

    public function gc($maxlifetime){
        $this->sessionExprireTime=$maxlifetime;
        return true;
    }


    public function __destruct(){
        session_write_close();
    }
}
?>